﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Forms;
using PocketPond.Core;
using System.IO;
using PocketPond.PondConsumer;

namespace PocketPond
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [MTAThread]
        static void Main()
        {
            while (true)
            {
                // Read config
                FileStream fs = new FileStream( Core.Utils.getPocketPondDir() +
                                                "\\settings", FileMode.OpenOrCreate);
                try
                {
                    StreamReader sr = new StreamReader(fs);
                    ClientSettings.RefreshInterval = int.Parse(sr.ReadLine().Trim());
                    ClientSettings.Username = sr.ReadLine().Trim();
                    ClientSettings.Password = sr.ReadLine().Trim();
                    ClientSettings.OnlyUnread = sr.ReadLine().Trim() == "true";

                    ClientSettings.AuthToken = PondRequest.getAuthtoken(ClientSettings.Username, ClientSettings.Password);
                    fs.Close();
                    break;
                }
                catch
                {
                    fs.Close();
                    MessageBox.Show("Your settings are invalid, please fix them");
                    Settings dlg = new Settings();
                    if (dlg.ShowDialog() == DialogResult.Cancel)
                    {
                        return;
                    }
                }
            }
            
            Application.Run(new PocketPond());
        }
    }
}